package com.le.tester.javaThink.fifth.chapter14_StreamingProgramming;

import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 描述：最后一个元素
 */
public class LastElement {
    public static void main(String[] args) {
        //如果必须选择流中的最后一个元素，那么应该使用的是reduce
        OptionalInt last = IntStream.range(10, 20).reduce((n1, n2) -> n2);

        System.out.println(last.orElse(-1));
        Optional<String> lastObj = Stream.of("one", "two", "three").reduce((n1, n2) -> n2);
        System.out.println(lastObj.orElse("Nothing Three"));
    }
}
